package com.shengruan.admin.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shengruan.admin.vo.PayParameter;
import com.shengruan.framework.jdbc.BaseJdbc;
import com.shengruan.framework.jdbc.BeanHandler;
import com.shengruan.framework.jdbc.BeanListHandler;

public class PayParameterJdbc extends BaseJdbc {

    Logger logger = LoggerFactory.getLogger(getClass());

    public static final String PAYPARAMETER_INSERT = "insert into payparameter (bidding, giftBidding, amount, creationTime)values(?, ?, ?, ?)";
    public static final String PAYPARAMETER_UPDATE = "UPDATE payparameter SET bidding = ? , giftBidding = ? , amount = ? , creationTime = ? WHERE id = ?";
    public static final String PAYPARAMETER_GETBYID = "select * from payparameter where id = ?";
    public static final String PAYPARAMETER_DELETEBYID = "delete from payparameter where id = ?";


    public void save(PayParameter payParameter) {
        Connection conn = this.getConnection();
        Object[] pram = {payParameter.getBidding(), payParameter.getGiftBidding(), payParameter.getAmount(), payParameter.getCreationTime()};
        try {
            this.execute(conn, PAYPARAMETER_INSERT, pram);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close(conn);
        }
    }


    public List<PayParameter> findAll() {
        List<PayParameter> payParameterList = new ArrayList<PayParameter>();
        Connection conn = this.getConnection();
		StringBuffer sql = new StringBuffer("SELECT * FROM payparameter order by bidding ");
		try {
			payParameterList = this.query(conn, sql.toString(), new BeanListHandler<PayParameter>(PayParameter.class));
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
		return payParameterList;
    }


   public void update(PayParameter payParameter) {
        Connection conn = this.getConnection();
        Object[] pram = {payParameter.getBidding(), payParameter.getGiftBidding(), payParameter.getAmount(), payParameter.getCreationTime(), payParameter.getId()};
        try {
			this.execute(conn, PAYPARAMETER_UPDATE, pram);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
    }


    public PayParameter getById(int id) {
        Connection conn = this.getConnection();
        PayParameter payParameter = null;
        try {
            payParameter = this.query(conn, PAYPARAMETER_GETBYID, new BeanHandler<PayParameter>(PayParameter.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close(conn);
		}
		return payParameter;
	}


    public void deleteById(int id){
		Connection conn = this.getConnection();
		try {
			this.execute(conn, PAYPARAMETER_DELETEBYID,new Object[]{id});
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(conn);
		}
	}
}
